package com.iw.adoms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.iw.adoms.dao.GoodsDao;
import com.iw.adoms.dao.MarchandiseDao;
import com.iw.adoms.entity.Goods;
import com.iw.adoms.entity.Marchandise;
import com.iw.core.orm.hibernate.BaseDao;
import com.iw.core.orm.hibernate.BaseService;

@Service
@Transactional
public class MarchandiseService extends BaseService<Marchandise> {
	@Override
	@Resource(name = "marchandiseDao")
	public void setBaseDao(BaseDao<Marchandise> baseDao) {
		this.baseDao = baseDao;
	}

	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private MarchandiseDao marchandiseDao;

	/**
	 * 查询当前部门的所有代理商品
	 * 
	 * @param departmentId
	 * @return
	 */
	public List<Marchandise> getAllMarchandiseByDepartMentId(Integer departmentId) {

		return marchandiseDao.getAllMarchandiseByDepartMentId(departmentId);

	}

	public void myStock(Model model, Integer departmentId, Integer mid) {

		List<Marchandise> mList;
		if (mid == null || mid == 0) {
			mList = this.getAllMarchandiseByDepartMentId(departmentId);
		} else {
			mList = new ArrayList<Marchandise>();
			Marchandise m = this.find(mid);
			mList.add(m);
		}

		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

		for (Marchandise marchandise : mList) {
			List<Goods> list = goodsDao.getStockListByMidAndDid(marchandise.getId(), departmentId);
			Map<String, Object> data = new HashMap<String, Object>();
			Integer nums = list.size();
			data.put("marchandise", marchandise.getName());
			data.put("mid", marchandise.getId());
			data.put("numOfBox", (nums / Double.valueOf(marchandise.getNumOfBox())));
			data.put("nums", nums);
			data.put("packageing", marchandise.getPackageing());
			data.put("spcification", marchandise.getSpcification());
			dataList.add(data);
		}
		model.addAttribute("dataList", dataList);
		model.addAttribute("mList", mList);

	}

	public void stockDetail(Model model, Integer departmentId, Integer mid) {
		Marchandise m = this.find(mid);
		String hql = "from Goods where marchandise.id=? and department.id = ?";
		@SuppressWarnings("unused")
		List<Object> dataList = new ArrayList<Object>();
		Map<Object, Object> data = new HashMap<Object, Object>();
		List<Goods> list = goodsDao.findList(hql, mid, departmentId);
		for (Goods g : list) {
			Map<String, Object> d = new HashMap<String, Object>();
			data.put(g.getInsertTime(), d);

		}
		model.addAttribute("data", data);

		model.addAttribute("goodsList", list);
		model.addAttribute("m", m);
	}

	public Integer getStockNumber(Integer mid, Integer departmentId) {
		List<Goods> list = goodsDao.getStockListByMidAndDid(mid, departmentId);
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}

	}

}
